#include "config.h"

Config::Config(QObject *parent)
    : QObject{parent}
{
    init_All_Data();

    QTime save_LockTime(lock_Hour().toInt(),lock_Minute().toInt(),0);
    QDate save_LockDate(lock_Year().toInt(),lock_Month().toInt(),lock_Day().toInt());
    lockTime.setTime(save_LockTime);
    lockTime.setDate(save_LockDate);

}

Config::~Config()
{
    save_All_Data();
}

void Config::init_All_Data()
{
    // qDebug()<<"构造函数触发!";
    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;
    QString fileName = QString("%1/config.ini").arg(currentDir);

    QSettings configIni(fileName, QSettings::IniFormat);

    configIni.beginGroup("PLC_Connect_Parameter");
    QString plc_Port = configIni.value("PLC_Port").toString();
    // qDebug()<<"读取出 PLC_Port 数据 : "<<plc_Port;
    setPlc_Port(plc_Port);
    QString plc_BaudRate = configIni.value("PLC_BaudRate").toString();
    // qDebug()<<"读取出 PLC_BaudRate 数据 : "<<plc_BaudRate;
    setPlc_BaudRate(plc_BaudRate);
    QString plc_DateBit = configIni.value("PLC_DataBit").toString();
    // qDebug()<<"读取出 PLC_DateBit 数据 : "<<plc_DateBit;
    setPlc_DataBit(plc_DateBit);
    QString plc_Parity = configIni.value("PLC_Parity").toString();
    // qDebug()<<"读取出 PLC_Parity 数据 : "<<plc_Parity;
    setPlc_Parity(plc_Parity);
    QString plc_StopBit = configIni.value("PLC_StopBit").toString();
    // qDebug()<<"读取出 PLC_StopBit 数据 : "<<plc_StopBit;
    setPlc_Stopbit(plc_StopBit);
    configIni.endGroup();

    configIni.beginGroup("Printer_Connect_Parameter");
    QString printer_Port = configIni.value("Printer_Port").toString();
    // qDebug()<<"读取出 Printer_Port 数据 : "<<printer_Port;
    setPrinter_Port(printer_Port);
    QString printer_BaudRate = configIni.value("Printer_BaudRate").toString();
    // qDebug()<<"读取出 Printer_BaudRate 数据 : "<<printer_BaudRate;
    setPrinter_BaudRate(printer_BaudRate);
    QString printer_DateBit = configIni.value("Printer_DataBit").toString();
    // qDebug()<<"读取出 Printer_DateBit 数据 : "<<printer_DateBit;
    setPrinter_DataBit(printer_DateBit);
    QString printer_Parity = configIni.value("Printer_Parity").toString();
    // qDebug()<<"读取出 Printer_Parity 数据 : "<<printer_Parity;
    setPrinter_Parity(printer_Parity);
    QString printer_StopBit = configIni.value("Printer_StopBit").toString();
    // qDebug()<<"读取出 Printer_StopBit 数据 : "<<printer_StopBit;
    setPrinter_Stopbit(printer_StopBit);
    configIni.endGroup();

    configIni.beginGroup("Scanner_Connect_Parameter");
    QString scanner_Port = configIni.value("Scanner_Port").toString();
    // qDebug()<<"读取出 Scanner_Port 数据 : "<<scanner_Port;
    setScanner_Port(scanner_Port);
    QString scanner_BaudRate = configIni.value("Scanner_BaudRate").toString();
    // qDebug()<<"读取出 Scanner_BaudRate 数据 : "<<scanner_BaudRate;
    setScanner_BaudRate(scanner_BaudRate);
    QString scanner_DateBit = configIni.value("Scanner_DataBit").toString();
    // qDebug()<<"读取出 Scanner_DateBit 数据 : "<<scanner_DateBit;
    setScanner_DataBit(scanner_DateBit);
    QString scanner_Parity = configIni.value("Scanner_Parity").toString();
    // qDebug()<<"读取出 Scanner_Parity 数据 : "<<scanner_Parity;
    setScanner_Parity(scanner_Parity);
    QString scanner_StopBit = configIni.value("Scanner_StopBit").toString();
    // qDebug()<<"读取出 Scanner_StopBit 数据 : "<<scanner_StopBit;
    setScanner_Stopbit(scanner_StopBit);
    configIni.endGroup();


    configIni.beginGroup("Lock_Data");
    QString lock_Year = configIni.value("Lock_Year").toString();
    // qDebug()<<"读取出 Lock_Year 数据 : "<<lock_Year;
    setLock_Year(lock_Year);
    QString lock_Month = configIni.value("Lock_Month").toString();
    // qDebug()<<"读取出 Lock_Month 数据 : "<<lock_Month;
    setLock_Month(lock_Month);
    QString lock_Day = configIni.value("Lock_Day").toString();
    // qDebug()<<"读取出 Lock_Day 数据 : "<<lock_Day;
    setLock_Day(lock_Day);
    QString lock_Hour = configIni.value("Lock_Hour").toString();
    // qDebug()<<"读取出 Lock_Hour 数据 : "<<lock_Hour;
    setLock_Hour(lock_Hour);
    QString lock_Minute = configIni.value("Lock_Minute").toString();
    // qDebug()<<"读取出 Lock_Minute 数据 : "<<lock_Minute;
    setLock_Minute(lock_Minute);
    // QString lock_Order = configIni.value("Lock_Order").toString();
    // qDebug()<<"读取出 Lock_Order 数据 : "<<lock_Order;
    // setLock_Order(lock_Order);
    // QString lock_Custom = configIni.value("Lock_Custom").toString();
    // qDebug()<<"读取出 Lock_Custom 数据 : "<<lock_Custom;
    // setLock_Custom(lock_Custom);
    configIni.endGroup();

    configIni.beginGroup("PrinterType");
    int printerType = configIni.value("Type").toInt();
    setPrinter_Type(printerType);
    configIni.endGroup();

    configIni.beginGroup("QR_CodeType");
    int QR_CodeType = configIni.value("Type").toInt();
    setQR_Code_Type(QR_CodeType);
    configIni.endGroup();

    configIni.beginGroup("NetWork_CodeType");
    int NetWork_CodeType = configIni.value("Type").toInt();
    setNetWork_QR_Type(NetWork_CodeType);
    configIni.endGroup();

    configIni.beginGroup("Language_Type");
    int Language_Type = configIni.value("Type").toInt();
    setLanguage_Type(Language_Type);
    configIni.endGroup();

}

void Config::save_All_Data()
{
    // qDebug()<<"析构函数触发!";
    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;

    QString fileName = QString("%1/config.ini").arg(currentDir);
    QSettings configIni(fileName, QSettings::IniFormat);


    // 设备连接数据
    configIni.beginGroup("PLC_Connect_Parameter");
    configIni.setValue("PLC_Port",plc_Port());
    configIni.setValue("PLC_BaudRate",plc_BaudRate());
    configIni.setValue("PLC_DataBit",plc_DataBit());
    configIni.setValue("PLC_Parity",plc_Parity());
    configIni.setValue("PLC_StopBit",plc_Stopbit());
    configIni.endGroup();
    configIni.beginGroup("Printer_Connect_Parameter");
    configIni.setValue("Printer_Port",printer_Port());
    configIni.setValue("Printer_BaudRate",printer_BaudRate());
    configIni.setValue("Printer_DataBit",printer_DataBit());
    configIni.setValue("Printer_Parity",printer_Parity());
    configIni.setValue("Printer_StopBit",printer_Stopbit());
    configIni.endGroup();
    configIni.beginGroup("Scanner_Connect_Parameter");
    configIni.setValue("Scanner_Port",scanner_Port());
    configIni.setValue("Scanner_BaudRate",scanner_BaudRate());
    configIni.setValue("Scanner_DataBit",scanner_DataBit());
    configIni.setValue("Scanner_Parity",scanner_Parity());
    configIni.setValue("Scanner_StopBit",scanner_Stopbit());
    configIni.endGroup();


    // 加密数据
    configIni.beginGroup("Lock_Data");
    configIni.setValue("Lock_Year",lock_Year());
    configIni.setValue("Lock_Month",lock_Month());
    configIni.setValue("Lock_Day",lock_Day());
    configIni.setValue("Lock_Hour",lock_Hour());
    configIni.setValue("Lock_Minute",lock_Minute());
    // configIni.setValue("Lock_Order",lock_Order());
    // configIni.setValue("Lock_Custom",lock_Custom());
    configIni.endGroup();

    // 打印机类型
    configIni.beginGroup("PrinterType");
    configIni.setValue("Type",Printer_Type());
    configIni.endGroup();

    // 二维码类型
    configIni.beginGroup("QR_CodeType");
    configIni.setValue("Type",QR_Code_Type());
    configIni.endGroup();

    // 在线料单二维码类型
    configIni.beginGroup("NetWork_CodeType");
    configIni.setValue("Type",NetWork_QR_Type());
    configIni.endGroup();

    // 当前语言类型
    configIni.beginGroup("Language_Type");
    configIni.setValue("Type",Language_Type());
    configIni.endGroup();

}

void Config::getPassWord(QString input_Passwd)
{
    qDebug()<<"输入的密码:   "<<input_Passwd;

    QStringList midString = input_Passwd.split("");

    // qDebug()<<"第一段:"<<midString.value(1)+midString.value(2)+midString.value(3)+midString.value(4);
    // qDebug()<<"第二段:"<<midString.value(5)+midString.value(6)+midString.value(7)+midString.value(8);
    // qDebug()<<"第三段:"<<midString.value(9)+midString.value(10)+midString.value(11)+midString.value(12);
    // qDebug()<<"第四段:"<<midString.value(13)+midString.value(14)+midString.value(15)+midString.value(16);
    // qDebug()<<"第五段:"<<midString.value(17)+midString.value(18)+midString.value(19)+midString.value(20);

    // setLock_Year()
    QString section_year = midString.value(1)+midString.value(2)+midString.value(3)+midString.value(4);
    QString section_month = midString.value(5)+midString.value(6);
    QString section_day =  midString.value(7)+midString.value(8);
    QString section_hour = midString.value(9)+midString.value(10);
    QString section_Minute = midString.value(11)+midString.value(12);
    // QString section_order = midString.value(13)+midString.value(14)+midString.value(15)+midString.value(16);
    // QString section_custom = midString.value(17)+midString.value(18)+midString.value(19)+midString.value(20);
    bool OK;
    // qDebug()<<"解码年限:"<<(section_year.toInt(&OK,16) / 2 - 10);
    // qDebug()<<"解码月份:"<<(section_month.toInt(&OK,16) - ((section_day.toInt(&OK,16) - 30 ) / 3) - 20);
    // qDebug()<<"解码日期:"<<((section_day.toInt(&OK,16) - 30 ) / 3);
    // qDebug()<<"解码小时:"<<(section_hour.toInt(&OK,16) - 40);
    // qDebug()<<"解码分钟:"<<(section_Minute.toInt(&OK,16) - 50);
    // qDebug()<<"解码序列号:"<<(section_order.toInt(&OK,16) - 24);
    // qDebug()<<"解码厂家码:"<<(section_custom.toInt(&OK,16) - 48);

    setLock_Year(QString("%1").arg((section_year.toInt(&OK,16) / 2 - 10)));
    setLock_Month(QString("%1").arg((section_month.toInt(&OK,16) - ((section_day.toInt(&OK,16) - 30 ) / 3) - 20)));
    setLock_Day(QString("%1").arg(((section_day.toInt(&OK,16) - 30 ) / 3)));
    setLock_Hour(QString("%1").arg((section_hour.toInt(&OK,16) - 40)));
    setLock_Minute(QString("%1").arg((section_Minute.toInt(&OK,16) - 50)));
    // setLock_Order(QString("%1").arg((section_order.toInt(&OK,16) - 24)));
    // setLock_Custom(QString("%1").arg((section_custom.toInt(&OK,16) - 48)));


}


QString Config::plc_Port() const
{
    return m_plc_Port;
}

void Config::setPlc_Port(const QString &newPlc_Port)
{
    if (m_plc_Port == newPlc_Port)
        return;
    m_plc_Port = newPlc_Port;
    emit plc_PortChanged();
}

QString Config::plc_BaudRate() const
{
    return m_plc_BaudRate;
}

void Config::setPlc_BaudRate(const QString &newPlc_BaudRate)
{
    if (m_plc_BaudRate == newPlc_BaudRate)
        return;
    m_plc_BaudRate = newPlc_BaudRate;
    emit plc_BaudRateChanged();
}

QString Config::plc_DataBit() const
{
    return m_plc_DataBit;
}

void Config::setPlc_DataBit(const QString &newPlc_DataBit)
{
    if (m_plc_DataBit == newPlc_DataBit)
        return;
    m_plc_DataBit = newPlc_DataBit;
    emit plc_DataBitChanged();
}

QString Config::plc_Parity() const
{
    return m_plc_Parity;
}

void Config::setPlc_Parity(const QString &newPlc_Parity)
{
    if (m_plc_Parity == newPlc_Parity)
        return;
    m_plc_Parity = newPlc_Parity;
    emit plc_ParityChanged();
}

QString Config::plc_Stopbit() const
{
    return m_plc_Stopbit;
}

void Config::setPlc_Stopbit(const QString &newPlc_Stopbit)
{
    if (m_plc_Stopbit == newPlc_Stopbit)
        return;
    m_plc_Stopbit = newPlc_Stopbit;
    emit plc_StopbitChanged();
}

QString Config::printer_Port() const
{
    return m_printer_Port;
}

void Config::setPrinter_Port(const QString &newPrinter_Port)
{
    if (m_printer_Port == newPrinter_Port)
        return;
    m_printer_Port = newPrinter_Port;
    emit printer_PortChanged();
}

QString Config::printer_BaudRate() const
{
    return m_printer_BaudRate;
}

void Config::setPrinter_BaudRate(const QString &newPrinter_BaudRate)
{
    if (m_printer_BaudRate == newPrinter_BaudRate)
        return;
    m_printer_BaudRate = newPrinter_BaudRate;
    emit printer_BaudRateChanged();
}

QString Config::printer_DataBit() const
{
    return m_printer_DataBit;
}

void Config::setPrinter_DataBit(const QString &newPrinter_DataBit)
{
    if (m_printer_DataBit == newPrinter_DataBit)
        return;
    m_printer_DataBit = newPrinter_DataBit;
    emit printer_DataBitChanged();
}

QString Config::printer_Parity() const
{
    return m_printer_Parity;
}

void Config::setPrinter_Parity(const QString &newPrinter_Parity)
{
    if (m_printer_Parity == newPrinter_Parity)
        return;
    m_printer_Parity = newPrinter_Parity;
    emit printer_ParityChanged();
}

QString Config::printer_Stopbit() const
{
    return m_printer_Stopbit;
}

void Config::setPrinter_Stopbit(const QString &newPrinter_Stopbit)
{
    if (m_printer_Stopbit == newPrinter_Stopbit)
        return;
    m_printer_Stopbit = newPrinter_Stopbit;
    emit printer_StopbitChanged();
}

QString Config::scanner_Port() const
{
    return m_scanner_Port;
}

void Config::setScanner_Port(const QString &newScanner_Port)
{
    if (m_scanner_Port == newScanner_Port)
        return;
    m_scanner_Port = newScanner_Port;
    emit scanner_PortChanged();
}

QString Config::scanner_BaudRate() const
{
    return m_scanner_BaudRate;
}

void Config::setScanner_BaudRate(const QString &newScanner_BaudRate)
{
    if (m_scanner_BaudRate == newScanner_BaudRate)
        return;
    m_scanner_BaudRate = newScanner_BaudRate;
    emit scanner_BaudRateChanged();
}

QString Config::scanner_DataBit() const
{
    return m_scanner_DataBit;
}

void Config::setScanner_DataBit(const QString &newScanner_DataBit)
{
    if (m_scanner_DataBit == newScanner_DataBit)
        return;
    m_scanner_DataBit = newScanner_DataBit;
    emit scanner_DataBitChanged();
}

QString Config::scanner_Parity() const
{
    return m_scanner_Parity;
}

void Config::setScanner_Parity(const QString &newScanner_Parity)
{
    if (m_scanner_Parity == newScanner_Parity)
        return;
    m_scanner_Parity = newScanner_Parity;
    emit scanner_ParityChanged();
}

QString Config::scanner_Stopbit() const
{
    return m_scanner_Stopbit;
}

void Config::setScanner_Stopbit(const QString &newScanner_Stopbit)
{
    if (m_scanner_Stopbit == newScanner_Stopbit)
        return;
    m_scanner_Stopbit = newScanner_Stopbit;
    emit scanner_StopbitChanged();
}

QString Config::inputPasswd() const
{
    return m_inputPasswd;
}

void Config::setInputPasswd(const QString &newInputPasswd)
{
    if (m_inputPasswd == newInputPasswd)
        return;
    m_inputPasswd = newInputPasswd;
    emit inputPasswdChanged();
}

QString Config::lock_Year() const
{
    return m_lock_Year;
}

void Config::setLock_Year(const QString &newLock_Year)
{
    if (m_lock_Year == newLock_Year)
        return;
    m_lock_Year = newLock_Year;
    emit lock_YearChanged();
}

QString Config::lock_Month() const
{
    return m_lock_Month;
}

void Config::setLock_Month(const QString &newLock_Month)
{
    if (m_lock_Month == newLock_Month)
        return;
    m_lock_Month = newLock_Month;
    emit lock_MonthChanged();
}

QString Config::lock_Day() const
{
    return m_lock_Day;
}

void Config::setLock_Day(const QString &newLock_Day)
{
    if (m_lock_Day == newLock_Day)
        return;
    m_lock_Day = newLock_Day;
    emit lock_DayChanged();
}

QString Config::lock_Hour() const
{
    return m_lock_Hour;
}

void Config::setLock_Hour(const QString &newLock_Hour)
{
    if (m_lock_Hour == newLock_Hour)
        return;
    m_lock_Hour = newLock_Hour;
    emit lock_HourChanged();
}

QString Config::lock_Minute() const
{
    return m_lock_Minute;
}

void Config::setLock_Minute(const QString &newLock_Minute)
{
    if (m_lock_Minute == newLock_Minute)
        return;
    m_lock_Minute = newLock_Minute;
    emit lock_MinuteChanged();
}

QString Config::lock_Order() const
{
    return m_lock_Order;
}

void Config::setLock_Order(const QString &newLock_Order)
{
    if (m_lock_Order == newLock_Order)
        return;
    m_lock_Order = newLock_Order;
    emit lock_OrderChanged();
}

QString Config::lock_Custom() const
{
    return m_lock_Custom;
}

void Config::setLock_Custom(const QString &newLock_Custom)
{
    if (m_lock_Custom == newLock_Custom)
        return;
    m_lock_Custom = newLock_Custom;
    emit lock_CustomChanged();
}

int Config::Printer_Type() const
{
    return m_Printer_Type;
}

void Config::setPrinter_Type(int newPrinter_Type)
{
    if (m_Printer_Type == newPrinter_Type)
        return;
    m_Printer_Type = newPrinter_Type;
    emit Printer_TypeChanged();
}

int Config::QR_Code_Type() const
{
    return m_QR_Code_Type;
}

void Config::setQR_Code_Type(int newQR_Code_Type)
{
    if (m_QR_Code_Type == newQR_Code_Type)
        return;
    m_QR_Code_Type = newQR_Code_Type;
    emit QR_Code_TypeChanged();
}

void Config::Get_Passwd_Need_To_Save(QString passwd)
{
    // getPassWord(passwd);
    qDebug()<<"配置文件 - 将要写入的新密码: "<<passwd;
    getPassWord(passwd);
}

int Config::Language_Type() const
{
    return m_Language_Type;
}

void Config::setLanguage_Type(int newLanguage_Type)
{
    if (m_Language_Type == newLanguage_Type)
        return;
    m_Language_Type = newLanguage_Type;
    emit Language_TypeChanged();
}

int Config::NetWork_QR_Type() const
{
    return m_NetWork_QR_Type;
}

void Config::setNetWork_QR_Type(int newNetWork_QR_Type)
{
    if (m_NetWork_QR_Type == newNetWork_QR_Type)
        return;
    m_NetWork_QR_Type = newNetWork_QR_Type;
    emit NetWork_QR_TypeChanged();
}
